﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Reflection;
using System.Collections;
using System.Data;
using Bronze.ReportBuilder.BusinessComponent;


namespace Bronze.ReportBuilder
{
    [Serializable]
    public class ReportDefinition
    {

        public CustomQuery Query
        {
            get;
            set;
        }

        public Dictionary<string, object> ReportParameters
        {
            get;
            set;
        }

        public FormDefinition Form
        {
            get;
            set;
        }

        /// <summary>
        /// 报表文件的标识，一般是Guid的字符串
        /// </summary>
        public string RdlcIdentify { get; set; }
    }

    [Serializable]
    public class CustomQuery
    {
        public string CommandText
        {
            get;
            set;
        }

        CommandType _commandType = CommandType.Text;
        public CommandType CommandType
        {
            get
            {
                return _commandType;
            }
            set
            {
                _commandType = value;
            }
        }

        public Dictionary<string, object> Parameters
        {
            get;
            set;
        }
    }

    [Serializable]
    public class FormDefinition
    {
        /// <summary>
        /// 执行统计的控件列表
        /// </summary>
        public List<string> PerformQueryControls
        {
            get;
            set;
        }

        public int Height
        { get; set; }

        public int Width
        {
            get;
            set;
        }

        Dictionary<string, int> _typeMap = new Dictionary<string, int>();

        public Dictionary<string, int> TypeMape
        {
            get { return _typeMap; }
            set { _typeMap = value; }
        }

        public List<WizardStepBase> Wizards
        { get; set; }

        private List<Element> _elements = new List<Element>();

        public string Name
        {
            get;
            set;
        }

        public List<Element> Elements
        {
            get { return _elements; }
            set { _elements = value; }
        }


        public static PropertyDescriptorCollection GetProperties(Gizmox.WebGUI.Forms.Component component)
        {
            return TypeDescriptor.GetProperties(component, new Attribute[] { new BrowsableAttribute(true) });
        }


        public List<Gizmox.WebGUI.Forms.Component> ToControls(ref Dictionary<Gizmox.WebGUI.Forms.Component, List<string>> trackedProperties)
        {
            List<Gizmox.WebGUI.Forms.Component> list = new List<Gizmox.WebGUI.Forms.Component>();
            foreach (var item in _elements)
            {
                list.Add(item.ToControl(this, ref trackedProperties));
            }
            return list;
        }

        public Type GetTypeName(int index)
        {
            foreach (var item in _typeMap)
            {
                if (item.Value == index)
                    return Type.GetType(item.Key);
            }
            return null;
        }

        public int GetTypeIndex(string typeName)
        {
            var typeIndex = 0;
            if (!this.TypeMape.ContainsKey(typeName))
            {
                typeIndex = this.TypeMape.Count;
                this.TypeMape[typeName] = typeIndex;
            }
            else
            {
                typeIndex = this.TypeMape[typeName];
            }
            return typeIndex;
        }



    }

    [Serializable]
    public class Element
    {
        public bool IsTrack { get; set; }

        public string Name { get; set; }

        public int TypeIndex { get; set; }

        private List<NameValue> _properties = new List<NameValue>();
        public List<NameValue> Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }

        public Gizmox.WebGUI.Forms.Component ToControl(FormDefinition form, ref Dictionary<Gizmox.WebGUI.Forms.Component, List<string>> trackedProperties)
        {
            return Utils.ToControl(form, this, ref trackedProperties);
        }


    }

    [Serializable]
    public class ElementList
    {
        //public string Type { get; set; }
        public int TypeIndex { get; set; }
        private List<Element> _elements = new List<Element>();

        public List<Element> Elements
        {
            get { return _elements; }
            set { _elements = value; }
        }
    }

    //[XmlInclude(typeof(Point))]
    //[XmlInclude(typeof(Size))]
    [Serializable]
    public class NameValue
    {
        public string Name { get; set; }

        public object Value { get; set; }
    }
}